﻿Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls


Public Class AddBeach
    Inherits System.Web.UI.Page

    Public Shared beachIDFinal As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Add_Activity.Visible = False
        Add_FoodDrink.Visible = False
        Add_Accommodation.Visible = False
        ReturnPanel.Visible = False
    End Sub

    Protected Sub btn_AddBeach_Click(sender As Object, e As EventArgs) Handles btn_AddBeach.Click

        'Make this more robust - if string is still empty after select case
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim ext As String = IO.Path.GetExtension(f_Picture.FileName)
        Dim newFileName As String = myGUID.ToString() + ext

        If (f_Picture.HasFile And isPicture(ext)) Then
            Dim picLocationOnHardDiskServer = Request.MapPath("Pictures") & "/" & newFileName
            f_Picture.SaveAs(picLocationOnHardDiskServer)

            'resizes an image to 100x100
            Dim img1 As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath("~/Pictures/") + newFileName)
            Dim bmp1 As System.Drawing.Image = img1.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
            bmp1.Save(MapPath("~/Thumbnails/") + newFileName)
            'resizes image to 400x400 for front page banner
            Dim bmp2 As System.Drawing.Image = img1.GetThumbnailImage(500, 400, Nothing, IntPtr.Zero)
            bmp2.Save(MapPath("~/Thumbnails/Banner/") + newFileName)
        Else
            Response.Redirect("AddBeach.aspx")
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO Beaches(BeachName, Region, Location, Description, Review, UserRating, Picture, Thumbnail, Tourist, DateAdded, BannerPicture, BlueFlag) VALUES (@f1, @f2, @f3, @f4, @f5, @f6, @F7, @F8, @f9, Now(), @f10, @f11)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_beachName.Text)
        cmd.Parameters.AddWithValue("@f2", ddl_Region.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", tb_Location.Text)
        cmd.Parameters.AddWithValue("@f4", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Review.Text)
        cmd.Parameters.AddWithValue("@f6", rbl_Rating.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f8", "Thumbnails/" & newFileName)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f10", "Thumbnails/Banner/" & newFileName)
        cmd.Parameters.AddWithValue("@f11", rbl_Flag.SelectedValue)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Find id of the new beach.
        Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Beaches ORDER BY ID DESC", oleDbConn)
        Dim dr = getLastIdCmd.ExecuteReader()
        dr.Read()
        beachIDFinal = dr.GetValue(0)

        Dim addRatingSql As String = "INSERT INTO BeachUserRating (Beach_FK, UserRating, Tourist) VALUES (@f1, @f2, @f3)"
        Dim addRatingCmd As New OleDbCommand(addRatingSql, oleDbConn)
        addRatingCmd.Parameters.AddWithValue("@f1", beachIDFinal)
        addRatingCmd.Parameters.AddWithValue("@f2", rbl_Rating.SelectedValue)
        addRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        addRatingCmd.ExecuteNonQuery()

        Add_Beach.Visible = False
        Add_Activity.Visible = True
        oleDbConn.Close()
        
    End Sub

    Protected Sub btn_AddActivity_Click(sender As Object, e As EventArgs) Handles btn_AddActivity.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()

        'Assossiate the beach id with the activity
        For Each cd In cbl_Activities.Items
            If cd.Selected = True Then
                Dim newSqlString As String = "INSERT INTO Activities(ActivityName, Beaches_FK) VALUES (@f1, @f2)"
                Dim newCmd As OleDbCommand = New OleDbCommand(newSqlString, oleDbConn)
                newCmd.CommandType = CommandType.Text
                newCmd.Parameters.AddWithValue("@f1", cd.Text)
                newCmd.Parameters.AddWithValue("@f2", beachIDFinal)
                newCmd.ExecuteNonQuery()
                newCmd.Dispose()
            End If
        Next
        If tb_Activity.Text = "" Then

        Else
            Dim newActivities = tb_Activity.Text.Split(",")
            For Each newActivity In newActivities
                Dim newSqlString As String = "INSERT INTO Activities(ActivityName, Beaches_FK) VALUES (@f1, @f2)"
                Dim newCmd As OleDbCommand = New OleDbCommand(newSqlString, oleDbConn)
                newCmd.CommandType = CommandType.Text
                newCmd.Parameters.AddWithValue("@f1", newActivity)
                newCmd.Parameters.AddWithValue("@f2", beachIDFinal)
                newCmd.ExecuteNonQuery()
                newCmd.Dispose()
            Next
        End If
        oleDbConn.Close()
        Add_Activity.Visible = False
        Add_FoodDrink.Visible = True
    End Sub

    Protected Sub btn_NoThanks_Click(sender As Object, e As EventArgs) Handles btn_NoThanks.Click
        Add_Activity.Visible = False
        Add_FoodDrink.Visible = True
    End Sub

    Protected Sub btn_AddFood_Click(sender As Object, e As EventArgs) Handles btn_AddFood.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim restaurantInt As Integer = 0
        Dim coffeeInt As Integer = 0
        Dim barInt As Integer = 0

        Dim countFood As Integer = 0
        For Each cd As ListItem In cbl_Food.Items
            If cd.Selected = True Then
                Select Case (cd.Value)
                    Case "Restaurant"
                        restaurantInt = -1
                    Case "Coffee"
                        coffeeInt = -1
                    Case "Bar"
                        barInt = -1
                End Select
                countFood += 1
            End If
        Next
        If countFood = 0 Then
            lbl_ChooseType.Text = "Choose a type"
            Add_FoodDrink.Visible = True
        Else
            Dim newSqlString As String = "INSERT INTO Cafes_Restaurant(CafeName, Restaurant, Coffee, Bar, Beaches_FK) VALUES (@f1, @f2, @f3, @f4, @f5)"
            Dim newCmd As OleDbCommand = New OleDbCommand(newSqlString, oleDbConn)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", tb_Food.Text)
            newCmd.Parameters.AddWithValue("@f2", restaurantInt)
            newCmd.Parameters.AddWithValue("@f3", coffeeInt)
            newCmd.Parameters.AddWithValue("@f4", barInt)
            newCmd.Parameters.AddWithValue("@f5", beachIDFinal)
            newCmd.ExecuteNonQuery()
            newCmd.Dispose()
            Add_FoodDrink.Visible = False
            Add_Accommodation.Visible = True
        End If
        oleDbConn.Close()
        
    End Sub

    Protected Sub btn_NoThanksFood_Click(sender As Object, e As EventArgs) Handles btn_NoThanksFood.Click
        Add_FoodDrink.Visible = False
        Add_Accommodation.Visible = True
    End Sub

    Protected Sub btn_Hotel_Click(sender As Object, e As EventArgs) Handles btn_Hotel.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "INSERT INTO Hotels(HotelName, isRecommended, Beach_FK, Facilities) VALUES (@f1, @f2, @f3, @f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_HotelName.Text)
        cmd.Parameters.AddWithValue("@f2", rbl_Hotel.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", beachIDFinal)
        cmd.Parameters.AddWithValue("@f4", tb_Facilities.Text)
        cmd.ExecuteNonQuery()
        oleDbConn.Close()
        Add_Accommodation.Visible = False
        ReturnPanel.Visible = True
    End Sub

    Protected Sub btn_NoThanksAccom_Click(sender As Object, e As EventArgs) Handles btn_NoThanksAccom.Click
        Add_Accommodation.Visible = False
        ReturnPanel.Visible = True
    End Sub

    Protected Sub btn_BackToList_Click(sender As Object, e As EventArgs) Handles btn_BackToList.Click

        Response.Redirect("BeachesList.aspx")

    End Sub

    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function
End Class